package com.locus.tools.location;



import android.location.*;
import android.content.Context;

/**
 * This class gets and does any necessary formatting to the users current location.
 * @author jfredett
 *
 */
public class LocalLocation implements ILocatable {
	
	/**
	 * The location manager, provides overarching location information
	 */
	LocationManager loc_manager;
	/**
	 * The Current Location
	 */
	Location current_location;
	// these could potentially be replaced by an intersection function, see below.
	/**
	 * the "best" provider according to the "crit" criteria
	 * @see Criteria crit
	 */
	LocationProvider provider;
	/**
	 *  the criteria for the best provider
	 */
	Criteria crit;
	String name;
	
	/**
	 * constructs a LocalLocation Instance with the given Context.
	 * @param acont
	 * @param name
	 */
	public LocalLocation(Context acont, String name){
		loc_manager = (LocationManager) acont.getSystemService("location");
		this.name = name;
		// Initialization 
		setCriteria(new Criteria());
		update();
	}
	/**
	 * Constructs a LocalLocation on the given context with the given provider criteria
	 * @param acont
	 * @param crit
	 * @param name 
	 */
	public LocalLocation(Context acont, Criteria crit, String name) {
		loc_manager = (LocationManager) acont.getSystemService("location");
		this.name = name;
		// Initialization
		setCriteria(crit);
		update();
	}
	
	/**
	 * Updates the current location.
	 */
	public void update() {
		current_location = loc_manager.getCurrentLocation(provider.getName());
	}

	/** 
	 * Updates the criteria to find a service provider.
	 * @param newcrit
	 */
	public void setCriteria(Criteria newcrit){
		this.crit = newcrit;
		this.provider = loc_manager.getBestProvider(crit);
	}
	
	/**
	 * Creates a map overlay.
	 */
	public LocationOverlay mkOverlay() {
		return (new LocationOverlay(current_location));
	}
	
	@Override
	public Location getLocation() {
		return current_location;
	}

	@Override
	public String getName() {
		return name;
	}
	
	
	
}
